var mainCtrl = myApp.controller('mainCtrl', ['$scope', '$rootScope', '$state', function($scope, $rootScope, $state) {
	$rootScope.secondType = "";
	$scope.name = "";
	$rootScope.user = {};
	// info
	$scope.msg = "";
	$scope.visible = false;
	/**
	 * 初始化方法
	 * @return {[type]}
	 */
	function init() {
		$(document).ready(function() {
			dropdownOpen();
			isLogin();
		});
	}
	init();
	
	// 目录hover时，下拉
	function dropdownOpen() {
		$(document).on('mouseover', 'li.dropdown', function() {
			$(this).addClass('open');
		}).on('mouseout', 'li.dropdown', function() {
			$(this).removeClass('open');
		});
	}

	/**
	 * 判断是否已经登录
	 * @return {Boolean} [description]
	 */
	function isLogin() {
		var user = Tools.cookie.get("user");
		if(Tools.reg.isNull(user)) {
			$scope.msg = "登录过期，请重新登录";
			$scope.visible = true;
			setTimeout(function () {
				window.location.href = "index.html";
			}, 1500);
		}
		user = JSON.parse(user);
		$rootScope.user = user;
		$scope.getPermissionList();
	};
	$scope.getPermissionList = function() {
		var data = {};
		commonSrv.getPermissionList(data, function(result) {
			if(result.status == 0) {
				// 正式
				// $rootScope.navList = result.data;
				// 测试
				// $rootScope.navList = Tools.permissionList;
				var href = window.location.href;
				if(href.indexOf('localhost:') >= 0) {
					$rootScope.navList = Tools.permissionList;
				} else {
					$rootScope.navList = result.data;					
				}
			}
		}, function(result) {
			alert("获取权限列表失败");
		}, function () {
			$scope.$apply();
		});
	};
	/**
	 * 登出系统
	 * @return {[type]} [description]
	 */
	$scope.logout = function() {
		commonSrv.logout({}, function(result) {
			if(result.status == 0) {
				Tools.cookie.del("user");
				window.location.href="index.html";
			}
		}, function(result) {
			alert("登出失败");
		}, function() {
			$scope.$apply();
		});
	};

	/**
	 * 进入首页
	 * @return {[type]} [description]
	 */
	$scope.goMain = function() {
		// setNavActive();
		$('.nav-list-item').removeClass('active');
		$state.go('index');
	};
}]);